/**
 * 
 */
package it.nola.soccer.listener;

import it.nola.soccer.DbAdapter;
import it.nola.soccer.DbConst;
import it.nola.soccer.GoalEdit;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

/**
 * @author paolo
 */
public class TeamGoalItemListener implements OnItemSelectedListener {
    Spinner playerSpinner;
    DbAdapter dba;
    long cronaca;
    GoalEdit goalEdit;

    public TeamGoalItemListener(GoalEdit ge) {
	goalEdit=ge;
	playerSpinner=ge.getPlayerSpinner();
	dba=ge.getDbHelper();
	cronaca=ge.getCronaca();
    }

    /**
     * Abilita i campi player in e player out e riempie con i valori di binding
     * 
     * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView,
     *      android.view.View, int, long)
     */
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {
	playerSpinner.setEnabled(true);
	Cursor playerCur=dba.getAllPlayers(cronaca, pos+1); // pos 0 => sqaudra
							    // 1; pos =1 => //
							    // squadra=2
	SimpleCursorAdapter playerAdapter=new SimpleCursorAdapter(goalEdit, android.R.layout.simple_spinner_item,
	    playerCur, new String[]{DbConst.players.KEY_NAME }, new int[]{android.R.id.text1 });
	playerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	playerSpinner.setAdapter(playerAdapter);
    }

    /**
     * Disabilita i campi player in e player out e cancella i valori di binding
     * 
     * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.
     *      AdapterView )
     */
    public void onNothingSelected(AdapterView<?> arg0) {
	playerSpinner.setEnabled(false);
    }

}
